import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse, HttpHeaders } from '@angular/common/http';
import { Event } from './incident.model';
import { ConfigService } from 'app/config/config.service';
import { INCIDENT_API_URL } from 'app/app.constants';
import { LoginService } from 'app/auth/login.service';
import { PublicService } from 'app/public/public.service';
import { Subject, Observable, BehaviorSubject } from 'rxjs';
import { filter } from 'rxjs/operators';

@Injectable({
    providedIn: 'root'
})
export class IncidentService {
    private apiUrl_equip_online = this.config.apiUrl.user;
    public events: Event[] = [];
    public resourceUrl = this.config.apiUrl.incident;
    public _uploadBase64 = this.config.apiUrl.uploadBase64;
    private apiUrl_vms_online = this.config.apiUrl.vms;
    public resourceUrlResponsePlan = this.config.apiUrl.responseplan;
    private zone = this.config.apiUrl.zone;
    // public resourceUrl = INCIDENT_API_URL;
    headerOptions = new HttpHeaders({
        Authorization: this.ls.getAccessToken()
    });

    constructor(private http: HttpClient, private config: ConfigService, private ls: LoginService, private pubSer: PublicService) {}

    public renderHeader(arr2, arr3) {
        return [
            {
                name: 'ID',
                key: null,
                value: 'ir_id',
                isChecked: true
            },
            {
                name: 'Type',
                key: null,
                value: 'ir_type',
                isChecked: true
            },
            {
                name: 'Start Date',
                key: null,
                value: 'start_Time',
                isChecked: true
            },
            {
                name: 'Planned Time To Clear',
                key: null,
                value: 'end_Time',
                isChecked: true
            },
            {
                name: 'Zone ID',
                key: null,
                value: 'zone_ID',
                isChecked: true
            },
            {
                name: 'Road Name',
                key: null,
                value: 'road_Name',
                isChecked: true
            },
            {
                name: 'Lane Blockage',
                key: null,
                value: 'lane_Blockage',
                isChecked: true
            },
            {
                name: 'Source',
                key: null,
                value: 'source',
                isChecked: true,
                selectList: arr2
            },
            {
                name: 'Status',
                key: null,
                value: 'veri_State',
                isChecked: true,
                selectList: arr3
            }
        ];
    }
    public hisRenderHeader(arr2, arr3) {
        return [
            {
                name: 'ID',
                key: null,
                value: 'ir_id',
                isChecked: true
            },
            {
                name: 'Type',
                key: null,
                value: 'ir_type',
                isChecked: true
            },
            {
                name: 'Start Date',
                key: null,
                value: 'start_Time',
                isChecked: true
            },
            {
                name: 'End Date',
                key: null,
                value: 'end_time',
                isChecked: true
            },
            {
                name: 'Planned Time To Clear',
                key: null,
                value: 'end_Time',
                isChecked: true
            },
            {
                name: 'Zone ID',
                key: null,
                value: 'zone_ID',
                isChecked: true
            },
            {
                name: 'Road Name',
                key: null,
                value: 'road_Name',
                isChecked: true
            },
            {
                name: 'Lane Blockage',
                key: null,
                value: 'lane_Blockage',
                isChecked: true
            },
            {
                name: 'Source',
                key: null,
                value: 'source',
                isChecked: true,
                selectList: arr2
            },
            {
                name: 'Status',
                key: null,
                value: 'veri_State',
                isChecked: true,
                selectList: arr3
            }
        ];
    }
    // public SOURCESELECTLIST() {
    //     return [
    //         { name: 'All', value: null },
    //         { name: 'Surveillance Camera', value: '0' },
    //         { name: 'Police Patrol', value: '1' },
    //         { name: 'Call In', value: '2' },
    //         { name: 'Automatically Detect Accident', value: '3' },
    //         { name: 'Others', value: '4' }
    //     ];
    // }
    // public STATUSSELECTLIST() {
    //     return [
    //         { name: 'All', value: null },
    //         { name: 'Anticipated', value: '0' },
    //         { name: 'Notified', value: '1' },
    //         { name: 'Confirmed', value: '2' },
    //         { name: 'In Progress', value: '3' },
    //         { name: 'Over', value: '4' }
    //     ];
    // }
    getIncidentRecordList(data) {
        return this.http.post<any>(this.resourceUrl + '/vnqnincidentmgt/getIRByParam', data);
    }
    getIncidentIRTowByIrSubId(data) {
        return this.http.get<any>(this.resourceUrl + '/vnqnincidentmgt/getIRTowByIrSubId?irSubId=' + data);
    }
    updateIRTow(data) {
        return this.http.post<any>(this.resourceUrl + '/vnqnincidentmgt/updateIRTow', data);
    }
    getIncidentRecordHistoryList(data) {
        return this.http.post<any>(this.resourceUrl + '/vnqnincidentmgt/getHistIRByParam', data);
    }
    createIrDetail(data: any, header: any) {
        let headers = new HttpHeaders();
        headers = headers.append('Authorization', this.ls.getAccessToken());
        headers = headers.append('isForce', header.isFource);
        headers = headers.append('zoneId', header.zoneId);
        return this.http.post<any>(this.resourceUrl + '/vnqnincidentmgt/createIR', data, { headers });
    }
    // 获取用户列表
    getAllUser(param): Observable<any> {
        // const url = `${this.apiUrl_equip_online}/userMgt/user/getAllUser`;
        return this.http.post<any>(this.apiUrl_equip_online + '/userMgt/user/getAllUserInfoByPage', param);
    }
    updateIrDetail(data: any, header: any) {
        let headers = new HttpHeaders();
        headers = headers.append('Authorization', this.ls.getAccessToken());
        headers = headers.append('isForce', header.isFource);
        headers = headers.append('zoneId', header.zoneId);
        return this.http.post<any>(this.resourceUrl + '/vnqnincidentmgt/updateIR', data, { headers });
    }
    finishIrDetail(data: any, header: any) {
        let headerSend = new HttpHeaders();
        headerSend = headerSend.append('Authorization', this.ls.getAccessToken());
        headerSend = headerSend.append('isForce', header.isFource);
        headerSend = headerSend.append('zoneId', header.zoneId);
        return this.http.post<any>(this.resourceUrl + '/vnqnincidentmgt/finishIR', data, { headers: headerSend });
    }
    getIncidentResponsePlanList(data) {
        return this.http.post<any>(this.resourceUrl + '/vnqnincidentmgt/getIPByParam', data);
    }
    getResponsePlanList(data) {
        return this.http.get<any>(this.resourceUrlResponsePlan + '/resplan/caserecord/findbyid/' + data);
    }
    executeResponsePlan(data) {
        return this.http.get<any>(this.resourceUrl + '/vnqnincidentmgt/activateIP?ipID=' + data);
    }
    updateResponsePlan(data) {
        return this.http.post<any>(this.resourceUrl + '/vnqnincidentmgt/updateIP', data);
    }
    deleteResponsePlan(data) {
        return this.http.get<any>(this.resourceUrl + '/vnqnincidentmgt/deleteIPCmd?cmdId=' + data);
    }
    // 删除事件协作信息
    deleteIrPartner(data) {
        return this.http.delete<any>(this.resourceUrl + '/vnqnincidentmgt/deleteIrPartnerById?partnerId=' + data);
    }
    // 保存事件协作信息
    saveOrUpdateIRPartner(data) {
        return this.http.post<any>(this.resourceUrl + '/vnqnincidentmgt/saveOrUpdateIRPartner', data);
    }
    // 根据事件ID获取当前应急事件
    getIRById(data) {
        return this.http.get<any>(this.resourceUrl + '/vnqnincidentmgt/getIRById?irID=' + data);
    }
    // 删除事件评论
    deleteIRComments(data) {
        return this.http.delete<any>(this.resourceUrl + '/vnqnincidentmgt/deleteIRComment?id=' + data);
    }

    // 修改事件评论
    saveOrUpdateIRComment(data) {
        return this.http.post<any>(this.resourceUrl + '/vnqnincidentmgt/saveOrUpdateIRComment', data);
    }

    // // 根据事件ID获取事件评论
    // getIRCommentByIRID(data) {
    //     return this.http.get<any>(this.resourceUrl + '/vnqnincidentmgt/getIRCommentByIRID?irID=' + data);
    // }
    // data
    formIncidentRecordListData(array, dataRange, clearDataRange, irType, pageParam, zoneIdList) {
        const data = {
            condition: {
                e_Time: {
                    endTime: null,
                    startTime: null
                },
                ir_id: null,
                ir_type: null,
                start_Time: {
                    endTime: null,
                    startTime: null
                },
                zoneIdList
            },
            pageNum: pageParam.pageNo,
            pageSize: pageParam.pageSize
        };
        array.forEach((item, i) => {
            // tslint:disable-next-line:prefer-const
            let { key, value } = item;
            if (value === 'ir_type') {
                key = irType;
            }
            if (key === '') {
                key = null;
            }
            if (value !== 'start_Time' && value !== 'end_Time') {
                data.condition[value] = key;
            }
        });
        data.condition.start_Time.startTime = dataRange.startTime;
        data.condition.start_Time.endTime = dataRange.endTime;
        data.condition.e_Time.startTime = clearDataRange.startTime;
        data.condition.e_Time.endTime = clearDataRange.endTime;
        return data;
    }
    uploadBase64(data): Observable<any> {
        // const url = `${this.apiUrl_vms_online}/uploadBase64`;
        const url = `${this._uploadBase64}/vnqnvmsmgt/uploadBase64List`;
        return this.http.post<any>(url, data);
    }
    // params = { posX: longitude, posY: latitude }
    // http://192.168.0.84:9586/vnqnzonemgt/vnqnzonemgt/zonemgt/getZoneIdByPoint
    getZoneIdFromGis(params): Observable<any> {
        const url = `${this.zone}/vnqnzonemgt/zonemgt/getZoneIdByPoint`;
        return this.http.post<any>(url, params);
    }
}
